/* GUI CONVERTED from sosocoa.p (converter v1.76) Fri Sep  6 04:06:02 2002 */
/* sosocoa.p - CONFIRM SALESORDER - SUBROUTINE                                */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                        */
/* All rights reserved worldwide.  This is an unpublished work.               */
/* $Revision: 1.41 $                                                          */
/*V8:ConvertMode=Report                                                       */
/*  REVISION: 5.0       LAST EDIT: 10/31/88      MODIFIED BY: emb *B003*      */
/*  REVISION: 5.0       LAST EDIT: 11/11/88      MODIFIED BY: emb *B003*      */
/*  REVISION: 5.0       LAST EDIT: 04/03/89      MODIFIED BY: emb *B084*      */
/*  REVISION: 5.0       LAST MODIFIED: 06/23/89  MODIFIED BY: MLB *B159*      */
/*  REVISION: 5.0       LAST MODIFIED: 12/08/89  MODIFIED BY: ftb *B433*      */
/*  REVISION: 5.0       LAST MODIFIED: 02/20/90  MODIFIED BY: emb *B578*      */
/*  REVISION: 5.0       LAST MODIFIED: 04/12/90  MODIFIED BY: emb *B662*      */
/*  REVISION: 5.0       LAST MODIFIED: 08/13/90  MODIFIED BY: emb *B763*      */
/*  REVISION: 6.0       LAST MODIFIED: 05/22/90  MODIFIED BY: WUG *D022*      */
/*  REVISION: 6.0       LAST MODIFIED: 08/30/90  MODIFIED BY: EMB *D040*      */
/*  REVISION: 6.0       LAST MODIFIED: 07/26/91  MODIFIED BY: afs *D792*      */
/*  REVISION: 7.0       LAST MODIFIED: 10/11/91  MODIFIED BY: emb *F024*      */
/*  REVISION: 7.0       LAST MODIFIED: 04/09/92  MODIFIED BY: emb *F369*      */
/*  REVISION: 7.0       LAST MODIFIED: 06/10/92  MODIFIED BY: tjs *F504*      */
/*  REVISION: 7.2       LAST MODIFIED: 02/07/94  MODIFIED BY: afs *FL96*      */
/* Oracle changes (share-locks)        09/13/94           BY: rwl *FR31*      */
/*  REVISION: 7.4       LAST MODIFIED: 01/17/95  MODIFIED BY: dpm *F0F7*      */
/*  REVISION: 7.4       LAST MODIFIED: 01/26/95  MODIFIED BY: bcm *F0G8*      */
/*  REVISION: 7.4       LAST MODIFIED: 04/03/95  MODIFIED BY: rxm *F0PP*      */
/*  REVISION: 7.4       LAST MODIFIED: 05/05/95  MODIFIED BY: rxm *F0R8*      */
/*  REVISION: 7.4       LAST MODIFIED: 06/14/95  MODIFIED BY: jym *G0PG*      */
/*  REVISION: 7.4       LAST MODIFIED: 10/10/95  MODIFIED BY: jym *G0YT*      */
/*  REVISION: 8.6   LAST MODIFIED: 07/02/96  MODIFIED BY: *K004* Kurt De Wit  */
/*  REVISION: 8.6   LAST MODIFIED: 11/05/96  MODIFIED BY: *K01T* Kurt De Wit  */
/*  REVISION: 8.6   LAST MODIFIED: 02/14/97  MODIFIED BY: *G2KY* Ajit Deodhar */
/*  REVISION: 8.6   LAST MODIFIED: 04/29/97  MODIFIED BY: *J1PY* Sanjay Patil */
/*  REVISION: 8.6   LAST MODIFIED: 05/30/97  MODIFIED BY: *K0DT* Arul Victoria*/
/*  REVISION: 8.6   LAST MODIFIED: 07/06/97  MODIFIED BY: *K0FM* Kieu Nguyen  */
/*  REVISION: 8.6   LAST MODIFIED: 07/08/97  MODIFIED BY: *K0DH* Jim Williams */
/*  REVISION: 8.6   LAST MODIFIED: 08/25/97  MODIFIED BY: *J1YJ* Aruna Patil  */
/*  REVISION: 8.6   LAST MODIFIED: 09/23/97  MODIFIED BY: *K0JH* Kieu Nguyen  */
/*  REVISION: 8.6   LAST MODIFIED: 10/13/97  MODIFIED BY: *K0W8* Kieu Nguyen  */
/*  REVISION: 8.6   LAST MODIFIED: 11/06/97  MODIFIED BY: *K18B* Jim Williams */
/*  REVISION: 8.6   LAST MODIFIED: 11/19/97  MODIFIED BY: *J26L* Mandar K.    */
/*  REVISION: 8.6   LAST MODIFIED: 11/27/97  MODIFIED BY: *K15N* Jerry Zhou   */
/* REVISION: 8.6E     LAST MODIFIED: 02/23/98   BY: *L007* A. Rahane          */
/* REVISION: 8.6E     LAST MODIFIED: 05/20/98   BY: *K1Q4* Alfred Tan         */
/* REVISION: 8.6E     LAST MODIFIED: 08/10/98   BY: *L052* Seema Varma        */
/* REVISION: 8.6E     LAST MODIFIED: 08/17/98   BY: *K1WC* Jim Williams       */
/* REVISION: 8.6E     LAST MODIFIED: 10/04/98   BY: *J314* Alfred Tan         */
/* REVISION: 9.0      LAST MODIFIED: 08/26/98   BY: *M00D* Dharmesh Parekh    */
/* REVISION: 9.0      LAST MODIFIED: 03/13/99   BY: *M0BD* Alfred Tan         */
/* REVISION: 9.1      LAST MODIFIED: 12/14/99   BY: *N05D* Steve Nugent       */
/* REVISION: 9.1      LAST MODIFIED: 03/02/00   BY: *L0SZ* Manish K.          */
/* REVISION: 9.1      LAST MODIFIED: 03/24/00   BY: *N08T* Annasaheb Rahane   */
/* REVISION: 9.1      LAST MODIFIED: 08/07/00   BY: *L128* Manish K.          */
/* Revision: 1.32     BY: Kirti Desai          DATE: 04/10/01   ECO: *M13C*   */
/* Revision: 1.33     BY: Russ Witt            DATE: 09/21/01   ECO: *P01H*   */
/* Revision: 1.34     BY: Russ Witt            DATE: 11/01/01   ECO: *P02M*   */
/* Revision: 1.35     BY: Sandeep P.           DATE: 11/05/01   ECO: *M1MQ*   */
/* Revision: 1.36     BY: Julie Milligan       DATE: 12/21/01   ECO: *M1SF*   */
/* Revision: 1.37     BY: Santosh Rao           DATE: 12/22/01  ECO: *M1S6*   */
/* Revision: 1.38     BY: Julie Milligan        DATE: 03/13/02  ECO: *M12Q*   */
/* Revision: 1.39     BY: Ashwini G.            DATE: 03/22/02  ECO: *M1WY*   */
/* Revision: 1.40     BY: Ashwini G.            DATE: 06/18/02  ECO: *M1ZF*   */
/* $Revision: 1.41 $       BY: Nishit V              DATE: 09/04/02  ECO: *N1SY*   */

/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/


/*GUI preprocessor directive settings */
&SCOPED-DEFINE PP_GUI_CONVERT_MODE REPORT

{mfdeclre.i}
{gplabel.i} /* EXTERNAL LABEL INCLUDE */

/* CHANGES MADE TO THIS PROGRAM MAY ALSO NEED TO BE MADE */
/* TO PROGRAM fseocoa.p.                                 */

/* ********** Begin Translatable Strings Definitions ********* */

&SCOPED-DEFINE sosocoa_p_5 "Error Cause"
/* MaxLen: Comment: */

&SCOPED-DEFINE sosocoa_p_6 "Cause of Error"
/* MaxLen: Comment: */

&SCOPED-DEFINE sosocoa_p_9 "Order!Line"
/* MaxLen: 8 Comment: Order Number ! Order Line Number */

&SCOPED-DEFINE sosocoa_p_10 "Status!APO ATP Exception"
/* MaxLen: 50 Comment: Order Stauts ! APO ATP Exception error messages*/

/* ********** End Translatable Strings Definitions ********* */

{pxmaint.i}

define new shared variable qty           like sod_qty_ord.
define new shared variable eff_date      as date.
define new shared variable sod_recno     as recid.
define new shared variable prev_consume  like sod_consume.
define new shared variable prev_due      like sod_due_date.
define new shared variable prev_qty_ord  like sod_qty_ord.
define new shared variable prev_type     like sod_type.
define new shared variable prev_site     like sod_site.
define new shared variable part          like sod_part.
define new shared variable sob_recno as recid.
define new shared variable remote_sod as recid.
define new shared variable update_mrp like mfc_logical.
define new shared variable so_recno      as recid.
define new shared variable sodreldate    like sod_due_date.
define new shared variable so_db         like dc_name.
define new shared variable change_db     like mfc_logical.
define new shared variable all_days      as integer.
define new shared variable config_lines  like mfc_logical.
define new shared variable new_line       like mfc_logical.

define new shared stream apoAtpStream.

define     shared variable sonbr         like so_nbr.
define     shared variable sonbr1        like so_nbr.
define     shared variable socust        like so_cust.
define     shared variable socust1       like so_cust.
define     shared variable socmclass     like cm_class.
define     shared variable socmclass1    like cm_class.
define     shared variable soship        like so_ship.
define     shared variable soship1       like so_ship.
define     shared variable soorddate     like so_ord_date.
define     shared variable soorddate1    like so_ord_date.
define     shared variable soduedate     like so_due_date.
define     shared variable soduedate1    like so_due_date.
define     shared variable site          like sod_site.
define     shared variable site1         like sod_site.
define     shared variable soallocate    like mfc_logical.
define     shared variable edi-conf         as logical.
define     shared variable soatpwarn     like mfc_logical.
define     shared variable soatperr      like mfc_logical.
define     shared variable sopromdate    like mfc_logical.
define     shared variable soUseStdAtpWhenNoApo like mfc_logical.

define            variable apoAtpDelAvail as logical no-undo
   initial yes.
define            variable apoAtpDelAvailMsg as integer no-undo.
define            variable qty_req       like in_qty_req.
define            variable trnbr         like tr_trnbr.
define            variable i             as integer.
define            variable lines_confirmed like mfc_logical.
define            variable open_qty      like mrp_qty.
define            variable sobconsume    like sob_qty_req.
define            variable sobabnormal   like sob_qty_req.
define            variable soc_sob_fcst  as character.
define            variable week          as integer.
define            variable so_head       like mfc_logical.
define            variable db-error      as logical no-undo.
define            variable txt            as character format "x(20)".
define            variable pm-code        like pt_pm_code.
define            variable pri-due-date   like sod_due_date.
define            variable sec-due-date   like sod_due_date.
define            variable exp-del-date   like sod_exp_del.
define            variable err-flag       as integer no-undo.
define            variable all_confrm     as logical no-undo.
define            variable p-po-so-hold   like po_so_hold.
define            variable p-so-nbr       like so_nbr.
define            variable p-sod-line     like sod_line.
define            variable p-sod-site     like sod_site.
define            variable p-sod-btb-vend like sod_btb_vend.
define            variable p-sod-qty-ord  like sod_qty_ord.
define            variable p-sod-due-date like sod_due_date.
define            variable p-po-nbr       like po_nbr.
define            variable p-pod-line     like pod_line.
define            variable p-pod-due-date like pod_due_date.
define            variable p-ship-to      like so_ship.
define            variable btb-type       like sod_btb_type.
define            variable btb-vend       like sod_btb_vend.
define            variable doc-type         as character.
define            variable doc-ref          as character.
define            variable add-ref          as character.
define            variable msg-type         as character.
define            variable trq-id         like trq_id.
define            variable vdemt            as logical no-undo.
define            variable ptemt            as logical no-undo.
define            variable ptpemt           as logical no-undo.
define            variable return-msg     like msg_nbr no-undo.
define            variable po-amended       as logical.
define            variable btb_type_desc    like glt_desc no-undo.
define            variable l_sod_recno      as recid no-undo.
define            variable l_so_recno       as recid no-undo.
define            variable l_msg       as character format "x(100)" no-undo.
define            variable l_atp_msg_needed as logical no-undo.
define            variable somstrLocked     as logical no-undo.
define            variable confirm-allowed  as logical no-undo.
define            variable prev-due-date    as date no-undo.
define            variable auto-prom-date   like sod_promise_date    no-undo.
define            variable invalid          as logical no-undo.
define            variable err_cause       as character format "x(18)" no-undo.
define            variable apoAtpOn         like mfc_logical no-undo.
define            variable moduleGroup      as character no-undo initial "SO".
define            variable demandId         as character no-undo.
define            variable messageNumber    as integer   no-undo.
define            variable emtSearchValue   as character no-undo.

define              buffer laddet          for lad_det.
define              buffer soddet          for sod_det.

/* APO ATP Global Defines */
{giapoatp.i}

/* Temp table used to store data returned from Adexa */
{giapott.i}
/* Define Handles for the programs. */
{pxphdef.i sosoxr1}
{pxphdef.i giapoxr}

define workfile btb-wkfl
   field    btb-nbr      like so_nbr
   field    btb-line     like sod_line
   field    btb-conf     like sod_confirm
   field    btb-req-date like sod_req_date
   field    btb-due-date like sod_due_date
   field    btb-type     like sod_btb_type
   field    btb-vend     like sod_btb_vend
   field    btb-mess-txt   as character format "x(50)".

define new shared workfile sobfile no-undo
   field sobpart like sob_part
   field sobsite like sob_site
   field sobissdate like sob_iss_date
   field sobqtyreq like sob_qty_req
   field sobconsume like sob_qty_req
   field sobabnormal like sob_qty_req.

/* Create temp-table for uprocessed SOB line items */
define temp-table unproc_file no-undo
   field unproc_db          like dc_name
   field unproc_nbr         like sob_nbr
   field unproc_line        like sob_line
   field unproc_part        like sob_part
   field unproc_site        like sob_site
   field unproc_cause       as character format "x(18)".

/*Definition of shared temp-table temp_sob*/
{sosobdef.i "new"}

/* define work files and temp tables */
define new shared workfile atpfile no-undo
  field atpdate like atp_date
  field atpqty  like atp_qty.

/* TEMP TABLE FOR ATP CALCULATIONS   */
{soatptt.i "new"}

define temp-table tt-atp-msg no-undo
    field atp-msg         like l_msg.

FORM /*GUI*/ 
   " * - "
   l_msg
with STREAM-IO /*GUI*/  frame msg_frm
width 132
no-labels
no-attr-space down.

FORM /*GUI*/ 
   skip
   txt
   skip
with STREAM-IO /*GUI*/  frame c no-labels width 132 no-attr-space.

FORM /*GUI*/ 
   space(8)
   btb-line
   btb-req-date
   btb-due-date
   btb-type
   btb-vend
   btb-mess-txt label {&sosocoa_p_5}
with STREAM-IO /*GUI*/  frame d width 132 no-attr-space.

/* SET EXTERNAL LABELS */
setFrameLabels(frame d:handle).

{mfdatev.i}
new_line = no.

/* SALES ORDER IN USE, ORDER IS NOT CONFIRMED */
{gprun.i ""gpmsg08.p"" "( 1778, 2, output l_msg)" }

assign
   txt      = getTermLabel("LINES_NOT_PROCESSED",19) + ":"
   eff_date = today
   so_db    = global_db.

find first soc_ctrl no-lock.

if soallocate then
   assign all_days = soc_ctrl.soc_all_days.

soc_sob_fcst = string(no).
{mfctrl01.i mfc_ctrl "soc_sob_fcst" soc_sob_fcst false}

if not edi-conf then do:
   {pxrun.i &proc = 'setUseApoAtpFlag' &program = 'sosoxr1.p'
         &handle=ph_sosoxr1
         &param = "(input moduleGroup,
                    input-output apoAtpOn)"
   }
   /* When coming from edi, there is no need to perform the APO ATP    */
   /* validation as that will be perfomred in the edi programs.        */
   if apoAtpOn then run setupApoAtp.
end. /* if not edi-conf (not coming  from EDI */

sodloop:
for each so_mstr no-lock
   where so_nbr >= sonbr and so_nbr <= sonbr1
     and so_cust >= socust and so_cust <= socust1
     and so_ship >= soship and so_ship <= soship1
     and so_ord_date >= soorddate and so_ord_date <= soorddate1
     and so_due_date >= soduedate and so_due_date <= soduedate1
     and so_fsm_type = "",
    each sod_det exclusive-lock
   where sod_nbr  = so_nbr
     and sod_site >= site
     and sod_site <= site1
     AND lookup(sod_site,global_user_site) > 0
     break by sod_nbr
           by sod_btb_vend
           by sod_btb_type
           by sod_site
           by sod_line:

   if first-of(sod_nbr)
   then do:

      for first cm_mstr
         fields (cm_addr cm_class)
         where cm_addr = so_cust
      no-lock:
         if cm_class < socmclass or cm_class > socmclass1
         then next sodloop.
      end.

      /*  CLEAR TEMP TABLE THAT HOLDS ATP ERROR MESSAGES  */
      for each tt-atp-msg exclusive-lock:
         delete tt-atp-msg.
      end.
      l_atp_msg_needed = no.

      so_head = yes.
      if so_conf_date <> ? then so_head = no.
      assign
         so_recno = recid(so_mstr)
         db-error = no.

      for each btb-wkfl exclusive-lock:
         delete btb-wkfl.
      end.

      lines_confirmed = no.

      /* FIND IF ANY OF DETAIL LINES NEED TO BE CONFIRMED */
      if can-find(first sod_det
                  where  sod_nbr  = so_nbr
                     and sod_site >= site
                     and sod_site <= site1
                     and sod_confirm = no)
      then
         lines_confirmed = yes.

      /* CHECK IF THERE ARE ANY OLD CONFIRMED LINES  */
      if lines_confirmed = no and so_head  then do:
         if can-find(first sod_det
                     where   sod_nbr  = so_nbr
                      and sod_confirm = yes)
         then
            lines_confirmed = yes.
      end. /* if lines_confirmed */
   end. /* IF FIRST-OF(sod_nbr) */

   /* UPDATE THE HEADER DATE AND DISPLAY IF LINES WERE CONFIRMEND */
   if lines_confirmed then do:

      run checkForSoMstrLocked
         (input so_nbr,
          output somstrLocked).

      if somstrLocked = yes then do:
         if (page-size - line-counter < 3) then page.

         if first-of(sod_nbr)
         then do:

            display
               so_mstr.so_nbr
               so_mstr.so_cust
               so_mstr.so_ship
               so_mstr.so_po
               so_mstr.so_ord_date
               so_mstr.so_req_date
               so_mstr.so_due_date
               "  *" @ so_mstr.so_conf_date
            with frame b width 132 no-attr-space down STREAM-IO /*GUI*/ .
            /* Inserted after display to solve frame outside
               its scope error */
            /* SET EXTERNAL LABELS */
            setFrameLabels(frame b:handle).

            display l_msg with frame msg_frm STREAM-IO /*GUI*/ .
         end. /* IF FIRST-OF(sod_nbr) */

         next sodloop.
      end. /* IF SOMSTRLOCKED */
   end. /* IF lines_confirmed THEN DO */

   if first-of(sod_nbr)
      and lines_confirmed
   then do:

      assign so_recno = recid(so_mstr).
      for each unproc_file exclusive-lock:
         delete unproc_file.
      end.
      assign all_confrm = yes.

      /* When coming from edi, only process EMT Types > 01        */
      /* The default value for emtSearchValue is blank            */
      /* When coming from edi and so_mstr header is not confirmed */
      /* then do not set all_confrm to yes.                       */
      if edi-conf then emtSearchValue = "02".
      if edi-conf and so_conf_date = ? then all_confrm = no.

   end. /* IF FIRST-OF(sod_nbr) AND lines_confirmed THEN DO  */

   if lines_confirmed then do :

      run checkForSoMstrLocked
         (input so_nbr,
          output somstrLocked).

      if not sod_confirm
      then do:

         if edi-conf and so_secondary and
            (sod_btb_type = "02" or sod_btb_type = "03") then do:

            find qad_wkfl where qad_key1 = sod_btb_vend and
               qad_key2 = "AUTO-EMT" no-lock no-error.
            if available qad_wkfl then
               vdemt = yes.
            else vdemt = no.

            find pt_mstr where pt_part = sod_part no-lock no-error.
            if available pt_mstr and pt__qad15 then
               ptemt = yes.
            else ptemt = no.

            find ptp_det where ptp_site = sod_site and
               ptp_part = sod_part no-lock no-error.
            if (available ptp_det and ptp__qad02 = 1) or
               not available ptp_det then
               ptpemt = yes.
            else ptpemt = no.

            if not soc__qad01 or not vdemt or not ptemt or not ptpemt
            then do:

               find first btb-wkfl where btb-nbr  = sod_nbr
                  and btb-line = sod_line
                  no-lock no-error.
               if not available btb-wkfl then do:
                  create btb-wkfl.
                  assign
                     btb-nbr      = sod_nbr
                     btb-line     = sod_line
                     btb-conf     = no
                     btb-req-date = sod_req_date
                     btb-due-date = sod_due_date
                     btb-type     = sod_btb_type
                     btb-vend     = sod_btb_vend
                     btb-mess-txt = dynamic-function
                                    ('getTermLabelFillCentered' in h-label,
                                    input "AUTO_EMT_NOT_ON",
                                    input 23,
                                    input "*").
               end.
               else
                  assign
                     btb-conf     = no
                     btb-mess-txt = dynamic-function
                                    ('getTermLabelFillCentered' in h-label,
                                    input "AUTO_EMT_NOT_ON",
                                    input 23,
                                    input "*").
               next sodloop.
            end.
         end.

         /* IT IS NECESSARY TO SET UP THE PREV_DUE DATE = SOD_DUE_DATE      */
         /* BECAUSE THE ROUTINE IN sosomti.p (WHICH IS USED BY SALES ORDER  */
         /* MAINT.) LOOKS TO SEE IF THE USER CHANGED THE DATE, AND IF SO    */
         /* THEN MAKES AN ADJUSTMENT FOR THE CHANGE.  IN SALES ORDER        */
         /* CONFIRM HOWEVER, THE USER DID NOT MAKE A CHANGE AND IF prev-due */
         /* IS NOT SET TO = sod_due_date, THEN UNNECESSARY PROCESSING WILL  */
         /* OCCUR.                                                          */
         /* FOR BTB SO THE ROUTINE sosomti.p IS NOT USED (NO CONFIGURED     */
         /* ITEMS) BUT WE STILL NEED THE OLD VALUE FOR THE DUE DATE BECAUSE */
         /* RECALCULATION IS NECESSARY WHEN THE FLAG soc_due_calc EQUALS    */
         /* YES (CHECKED IN sodueclc.p).                                    */

         assign prev_due = sod_due_date.

         find si_mstr where si_site = sod_site no-lock.
         if si_db > "" and si_db <> so_db
            and not connected(si_db) then do:
            db-error = yes.
               {pxmsg.i &MSGNUM=2510 &ERRORLEVEL=4 &MSGARG1=si_db}
            next sodloop.
         end.

         /* Start section to check all SOB components and switch to */
         /* remote database if necessary */
         assign
            invalid = no
            err_cause="" .
         for each temp_sob exclusive-lock:
            delete temp_sob.
         end.

         for each sob_det no-lock
            where sob_nbr = sod_det.sod_nbr
            and  sob_line = sod_det.sod_line:

            create temp_sob.
            assign
               temp_nbr = sob_nbr
               temp_line = sob_line
               temp_part = sob_part
               temp_site = sob_site
               temp_valid = no.
         end.

         if si_db <> so_db then do:
            /* Switch to remote Database if required */
            {gprun.i ""gpalias2.p"" "(sod_det.sod_site,output err-flag)"}
         end.

         if err-flag = 0
         then
            err_cause = getTermLabel("INVALID_COMPONENT",18).

         if err-flag = 2 or err-flag = 3
         then
            err_cause= getTermLabel("DB_CONNECTION_LOST",18).

         /* Check Components now that we have switched databases. */
         {gprun.i ""sqcheck.p""}

         if si_db <> so_db then do:
            {gprun.i ""gpalias3.p"" "(so_db,output err-flag)"}
         end.

         /* Write record to unprocessed table for each unprocessed item*/
         for each temp_sob where temp_valid = no no-lock:
            assign invalid = yes.
            create unproc_file.
            assign
               unproc_db    = (if si_db = "" then ldbname(1) else si_db)
               unproc_nbr   = temp_nbr
               unproc_line  = temp_line
               unproc_site  = temp_site
               unproc_part  = temp_part
               unproc_cause = err_cause.
         end.  /* for each */

         /* If an invalid component found then undo and leave */
         if invalid then do:
            assign all_confrm = no.
            undo sodloop, next sodloop.
         end.

         /* See if ATP Enforcement is in place and, if so, */
         /* see if due date needs to be modified and if    */
         /* order can be confirmed                         */
         prev-due-date = sod_due_date.

         run checkATP (output confirm-allowed).

         if confirm-allowed = no then do:
            assign all_confrm = no.
            undo sodloop, next sodloop.
         end.

         /* See if promise date should be changed (skip BTB) */
         if sopromdate = yes
            and sod_btb_type <= "01"
            and (sod_due_date <> prev-due-date
            /* (ATP Enforcement calculated a new Due Date) */
            or sod_promise_date = ?)
            /* (ATP Enforcement did not calculate a new Due Date, */
            /*  But there is no Promise Date yet) */
         then do:
            auto-prom-date = ?.
            run calcPromiseDate (input so_ship,
                                 input sod_site,
                                 input-output sod_due_date,
                                 input-output auto-prom-date).
            if auto-prom-date <> ? then
               sod_promise_date = auto-prom-date.
         end. /* sopromdate = yes */

         assign
            sod_confirm  = yes
            sod_recno    = recid(sod_det)
            config_lines = can-find(first sob_det where sob_nbr  = sod_nbr
                                    and sob_line = sod_line).

         /* CHECK TO SEE IF AT MBU AND PO EXISTS THEN PO HAS ALREADY */
         /* BEEN AMENDED IN edpocld.p NO NEED TO RUN socram01.p      */
         po-amended = no.
         if so_secondary and (sod_btb_type = "02" or sod_btb_type = "03")
         then do:
            find pod_det where pod_nbr = sod_btb_po and
               pod_line = sod_btb_pod_line no-lock no-error.
            if available pod_det then
               assign po-amended = yes.
         end.

         if so_primary and (sod_btb_type = "02" or sod_btb_type = "03")
            and soc_use_btb = yes and not po-amended
         then do:
            find first btb-wkfl where btb-nbr  = sod_nbr
               and btb-line = sod_line
               no-lock no-error.
            if not available btb-wkfl then do:
               create btb-wkfl.
               assign
                  btb-nbr      = sod_nbr
                  btb-line     = sod_line
                  btb-conf     = yes
                  btb-req-date = sod_req_date
                  btb-due-date = sod_due_date
                  btb-type     = sod_btb_type
                  btb-vend     = sod_btb_vend
                  btb-mess-txt = "".
            end.

            /* CHECK IF EXISTING PO CAN BE USED TO ADD LINE */
            if first-of(sod_btb_vend) or first-of(sod_btb_type)
               or (sod_btb_type = "02" and first-of(sod_site))
            then do:
               assign
                  btb-type = sod_btb_type
                  btb-vend = sod_btb_vend
                  p-po-nbr = "".

               if sod_btb_type = "03"
               then
                  find first soddet
                     where soddet.sod_nbr      = sod_det.sod_nbr
                     and soddet.sod_btb_vend   = sod_det.sod_btb_vend
                     and soddet.sod_btb_type   = sod_det.sod_btb_type
                     and soddet.sod_line      <> sod_det.sod_line
                     and soddet.sod_confirm    = yes
                     and soddet.sod_btb_po    <> ""
                     and soddet.sod_btb_pod_line <> 0
                     no-lock no-error.
               else
                  find first soddet
                     where soddet.sod_nbr      = sod_det.sod_nbr
                     and soddet.sod_btb_vend   = sod_det.sod_btb_vend
                     and soddet.sod_btb_type   = sod_det.sod_btb_type
                     and soddet.sod_site       = sod_det.sod_site
                     and soddet.sod_line      <> sod_det.sod_line
                     and soddet.sod_confirm    = yes
                     and soddet.sod_btb_po    <> ""
                     and soddet.sod_btb_pod_line <> 0
                     no-lock no-error.
               if available soddet then do:
                  /* MAKE SURE PO IS STILL THERE */
                  find pod_det where pod_nbr  = soddet.sod_btb_po
                     and pod_line = soddet.sod_btb_pod_line
                     no-lock no-error.
                  if available pod_det then do:

                     /* CHECK REJECTED OR SUPPLIER INITIATED CHANGE */
                     find first cmf_mstr
                        where  cmf_doc_type = "PO"
                          and  cmf_doc_ref  = soddet.sod_btb_po
                          and (cmf_status   = "2"  /* REJECTED */
                          or   cmf_status   = "3"  /* SUPPLIER */)
                          no-lock no-error.
                     if available cmf_mstr then
                        assign
                           btb-mess-txt    = getTermLabel
                           ("CHANGES_EXIST_WITH_TRANSACTION_NUM",39)
                           + ":" +
                           string(cmf_trans_nbr)
                           sod_det.sod_confirm = no
                           btb-conf            = no.
                     else do:

                        /* CHECK FOR PENDING CHANGE */
                        find first cmf_mstr
                           where cmf_doc_type = "PO"
                           and cmf_doc_ref  = soddet.sod_btb_po
                           and cmf_status   = "1" /* PENDING */
                           no-lock no-error.
                        if available cmf_mstr then do:
                           /* UNQUEUE DOCUMENT */
                           assign
                              doc-type = "PO"
                              doc-ref  = soddet.sod_btb_po
                              add-ref  = ""
                              msg-type = ""
                              trq-id   = 0.

                           {gprun.i ""gpquedoc.p""
                              "(input-output doc-type,
                                input-output doc-ref,
                                input-output add-ref,
                                input-output msg-type,
                                input-output trq-id,
                                input no )"}.

                        end.

                        assign p-po-nbr = soddet.sod_btb_po.
                     end.
                  end.
               end.
            end.

            if btb-mess-txt = "" then do:

               /* CALCULATE DUE DATE FOR BTB PRIMARY SO */
               assign p-pod-due-date = ?.

               /* ONLY CALC DUE DATE FOR EMT ORDER AND */
               /* DUE DATE CALCULATION FLAG IS YES     */
               find cm_mstr where cm_addr = so_cust no-lock no-error.
               if available cm_mstr and sod_det.sod_type = ""
                  and soc_due_calc and sod_det.sod_btb_type <> "01"
               then do:

                  {gprun.i ""sodueclc.p""
                     "(input  sod_det.sod_due_date,
                       input  sod_det.sod_part,
                       input  cm_addr,
                       input  sod_det.sod_btb_type,
                       input  sod_det.sod_site,
                       output sec-due-date,
                       output pri-due-date,
                       output exp-del-date,
                       input no)"}

                  assign
                     sod_det.sod_due_date   = pri-due-date
                     sod_det.sod_exp_del    = exp-del-date
                     p-pod-due-date         = sec-due-date
                     btb-due-date           = pri-due-date
                     sod_det.sod_per_date   = exp-del-date.

               end. /* if available cm_mstr and sod_det.sod_type = "" */

               if so_stat <> "" then p-po-so-hold = yes.
               else p-po-so-hold = no.

               if sod_det.sod_btb_type = "02"
               then assign p-ship-to = sod_det.sod_site.
               else assign p-ship-to = so_ship.

               assign
                  p-so-nbr       = so_nbr
                  p-sod-line     = sod_det.sod_line
                  p-sod-site     = sod_det.sod_site
                  p-sod-btb-vend = sod_det.sod_btb_vend
                  p-sod-qty-ord  = sod_det.sod_qty_ord
                  p-sod-due-date = sod_det.sod_due_date
                  p-pod-line     = 0.

               /* CREATE BTB PO FOR THIS SO */
               /* ADDED THE FOURTEENTH INPUT PARAMETER AS 'YES */
               {gprun.i ""socram01.p""
                  "(input p-so-nbr,
                    input p-sod-line,
                    input-output p-po-nbr,
                    input-output p-pod-line,
                    input no,
                    input no,
                    input p-sod-site,
                    input p-ship-to,
                    input p-sod-qty-ord,
                    input p-po-so-hold,
                    input p-pod-due-date,
                    input p-sod-due-date,
                    input yes,
                    input yes,
                    output return-msg)"}

               if return-msg <> 0
               then
                  assign
                     btb-mess-txt = getTermLabel
                                    ("CREATION_BTB_PO_NOT_SUCCESFUL",32)
                                    + ""
                                    + getTermLabel("ERROR",6)
                                    + ":"
                                    + string (return-msg)
                     sod_det.sod_confirm  = no
                     btb-conf             = no.

               else
                  assign
                     sod_det.sod_btb_po       = p-po-nbr
                     sod_det.sod_btb_pod_line = p-pod-line.

            end. /* btb-mess-txt = "" */

            if last-of(sod_det.sod_btb_type)
               or last-of(sod_det.sod_btb_vend)
               or (last-of(sod_det.sod_site) and
               sod_det.sod_btb_type = "02")
            then do:

               /* CHECK IF VENDOR IS AN EDI VENDOR. IF SO THEN QUEUE */
               /* DOCUMENT FOR TRANSMISSION. */

               find vd_mstr where vd_addr = sod_det.sod_btb_vend
                  no-lock no-error.
               find ad_mstr where ad_addr = sod_det.sod_btb_vend
                  no-lock no-error.
               if available ad_mstr and (ad_po_mthd = "e" or
                  ad_po_mthd = "b") and
                  available vd_mstr and (so_stat = "" or
                  (so_stat = "HD" and vd_rcv_held_so = yes)) and
                  p-po-nbr <> ""
               then do:

                  assign
                     doc-type = "PO"
                     doc-ref  = p-po-nbr
                     add-ref  = ""
                     trq-id   = 0.
                  find po_mstr where po_nbr = p-po-nbr no-lock no-error.
                  if available po_mstr then do:
                     if po_xmit = "1" then
                        msg-type = "ORDERS". /* NEW PO */
                     else
                        msg-type = "ORDCHG". /* PENDING CHANGE */

                     /* QUEUE DOCUMENT FOR TRANSMISSION - BTB */
                     {gprun.i ""gpquedoc.p""
                        "(input-output doc-type,
                          input-output doc-ref,
                          input-output add-ref,
                          input-output msg-type,
                          input-output trq-id,
                          input yes)"}.
                  end.

               end. /* if EDI-partner and document has to be queued */
            end. /* if last-of(sod_btb_type) or last-of(sod_btb_vend) */
         end. /* if so_primary and  BTB SO and config_lines = no */

         /* CHANGED ALL REFERENCES OF sod_det FILE THUS AVOIDING */
         /* PROBLEMS WITH BUFFER soddet. */

         /* NO BTB FOR CONFIGURED PRODUCTS THUS sod_confirm DOES NOT */
         /* HAVE TO BE CHECKED. */

         if config_lines then do:
            if sod_det.sod_status <> "FAS" and sod_det.sod_fa_nbr = ""
            then do:

               update_mrp = yes.
               for each sob_det where sob_nbr = sod_det.sod_nbr
                  and sob_line = sod_det.sod_line no-lock:

                  assign sob_recno = recid(sob_det).
                  {gprun.i ""sqqosou3.p""}
               end.

               assign
                  l_so_recno = so_recno
                  l_sod_recno = sod_recno.
               if change_db then do:
                  /* SWITCH TO THE INVENTORY SITE */
                  so_db = global_db .
                  {gprun.i ""gpalias2.p"" "(sod_det.sod_site,
                                            output err-flag  )"}
                  {gprun.i ""sqrecid.p"" "(sod_det.sod_nbr,
                                           sod_det.sod_line)"}
               end. /* IF CHANGE DB*/

               /* ADDED BELOW CODE TO CREATE */
               /* MRP RECORDS FOR COMPONENTS */
               if sod_det.sod_type = "" then do:
                  prev_due = sod_det.sod_due_date .
                  {gprun.i ""sosomti.p""}
               end. /* IF SOD_DET.SOD_TYPE  */

               if change_db then do:
                  {gprun.i ""gpalias3.p"" "(so_db , output err-flag)"}
                  assign
                     so_recno = l_so_recno
                     sod_recno = l_sod_recno.
               end. /* IF CHANGE_DB */

            end.
         end. /* if config_lines */

         /* RECONCILE DIRECT ALLOCATION FOR BTB SO */

         if so_secondary then do:
            find btb_det where btb_so       = so_nbr
               and btb_sod_line = sod_det.sod_line
               no-lock no-error.
            if available btb_det then do:
               for each lad_det
                  where lad_det.lad_dataset = "btb"
                    and lad_det.lad_nbr     = btb_pr_so
                    and lad_det.lad_line    = string(btb_pr_sod_line)
                    and lad_det.lad_part    = sod_det.sod_part
                    and lad_det.lad_site    = sod_det.sod_site
                    exclusive-lock:

                  /* DELETED CODE TO CHECK sod_btb_type TO SEE WHERE WE ARE  */
                  /* IN THE SUPPLY CHAIN.  IT DOESN'T MATTER WHAT LEVEL WE   */
                  /* ARE AT IF WE FIND AN EMT RESERVED ALLOCATION WE ALWAYS  */
                  /* WANT TO CONVERT IT.                                     */

                  if sod_det.sod_qty_all + lad_det.lad_qty_all >
                     sod_det.sod_qty_ord
                  then
                     assign
                        lad_det.lad_qty_all = sod_det.sod_qty_ord
                                              - sod_det.sod_qty_all.
                        sod_det.sod_qty_all = sod_det.sod_qty_all
                                              + lad_det.lad_qty_all.
                  find laddet
                     where laddet.lad_dataset = "sod_det"
                       and laddet.lad_nbr     = sod_det.sod_nbr
                       and laddet.lad_line    = string(sod_det.sod_line)
                       and laddet.lad_part    = sod_det.sod_part
                       and laddet.lad_site    = sod_det.sod_site
                       and laddet.lad_loc     = lad_det.lad_loc
                       and laddet.lad_lot     = lad_det.lad_lot
                       and laddet.lad_ref     = lad_det.lad_ref
                       exclusive-lock no-error.
                  if not available laddet
                  then
                     assign
                        lad_det.lad_dataset = "sod_det"
                        lad_det.lad_nbr     = sod_det.sod_nbr
                        lad_det.lad_line    = string(sod_det.sod_line).
                  else do:
                     assign
                        laddet.lad_qty_all = laddet.lad_qty_all +
                                             lad_det.lad_qty_all.
                     delete lad_det.
                  end.

               end. /* for each lad_det exclusive-lock */
            end. /* if available btb_det */
         end. /* if not so_primary */

         if sod_det.sod_confirm = yes
         then do:

            {gprun.i ""sqqosou1.p""}

            /* BACK OUT PLANNED FINAL ASSEMBLY (mrp_det) */
            /* ORDER FOR KIT PARENT ITEM OF EMT SO       */
            if (sod_det.sod_cfg_type = "2"
               and sod_det.sod_btb_po <> ""
               and can-find(soc_ctrl where soc_use_btb = yes))
            then do:
               {mfmrwdel.i
                  "sod_fas"
                   sod_det.sod_part
                   sod_det.sod_nbr
                   string(sod_det.sod_line)
                   """"}
            end. /* IF (sod_det.sod_cfg_type = "2" ...) */
         end. /* IF sod_det.sod_confirm = yes */
      end. /* IF NOT sod_confirm AND sod_btb_type >= emtSearchValue */

      if last-of(sod_det.sod_nbr)
      then do:
         if not can-find (first sod_det
                             where sod_nbr     = so_nbr
                               and sod_confirm = no )
         then
            all_confrm = yes.

         run updateOrderConfirmDate.

         /* NO DISPLAY WHEN CALLING FROM EDI */
         if edi-conf = no then do:
            if apoAtpOn then do:

               /* Change all APO ATP demand records to confirmed Status */
               for each tt-apoLinesConfirmed exclusive-lock:
                  /* Change the SCO Demand Record to confirmed status */

                  {pxrun.i &proc = 'buildDemandId'
                     &program = 'giapoxr.p'
                     &handle = ph_giapoxr
                     &param = "(input tt-apoLinesConfirmed.tt-orderId,
                                input tt-apoLinesConfirmed.tt-orderLineId,
                                input tt-apoLinesConfirmed.tt-siteId,
                                output demandId)"
                     &module = 'GI1'}

                  {pxrun.i &proc = 'confirmDemandRecord'
                     &program = 'giapoxr.p'
                     &handle = ph_giapoxr
                     &param = "(input tt-apoLinesConfirmed.tt-siteId,
                                input tt-apoLinesConfirmed.tt-itemId,
                                input demandId,
                                output messageNumber)"
                     &catcherror = TRUE
                     &noapperror = TRUE
                     &module = 'GI1'}

                  if (return-value <> {&SUCCESS-RESULT} and
                  tt-apoLinesConfirmed.tt-atpEnforcement <> "0")
                  then do:
                     /* This clears the temp table tt-apoExceptions */
                     {pxrun.i &proc = 'clearApoException'
                            &program = 'sosoxr1.p'
                            &handle=ph_sosoxr1
                            &catcherror = TRUE
                            &noapperror = TRUE}

                     {pxrun.i &proc = 'createApoExceptionAppend'
                        &program = 'sosoxr1.p'
                        &handle = ph_sosoxr1
                        &param = "(input tt-apoLinesConfirmed.tt-orderId,
                              input tt-apoLinesConfirmed.tt-orderLineId,
                              input tt-apoLinesConfirmed.tt-lineDueDate,
                              input messageNumber,
                              input '2',
                              input demandId,
                              input 43,
                              input-output table tt-apoExceptions)"}

                  end.

                  delete tt-apoLinesConfirmed.

               end.   /* for each tt-apoLinesConfirmed */

               do with frame b-apoatp:
                  /* SET EXTERNAL LABELS */
                  setFrameLabels(frame b-apoatp:handle).
                  display
                     so_mstr.so_nbr format "x(8)"
                        column-label {&sosocoa_p_9}
                     so_mstr.so_cust
                     so_mstr.so_ship
                     so_mstr.so_po
                     so_mstr.so_ord_date
                     so_mstr.so_req_date
                     so_mstr.so_due_date
                     so_mstr.so_conf_date
                     so_mstr.so_stat format "x(43)"
                        column-label {&sosocoa_p_10}
                  with frame b-apoatp width 132 no-attr-space down STREAM-IO /*GUI*/ .

                  /* Print any APO ATP exceptions */
                  for each tt-apoExceptions exclusive-lock
                     with frame b-apoatp width 132 no-attr-space down
                     break
                        by tt-apoExceptions.tt-orderId
                        by tt-apoExceptions.tt-orderLineId:

                     down 1 with frame b-apoatp.

                     if first-of(tt-apoExceptions.tt-orderLineId) then
                        display
                           tt-apoExceptions.tt-orderLineId @ so_mstr.so_nbr
                           tt-apoExceptions.tt-lineDueDate @ so_mstr.so_due_date
                           tt-apoExceptions.tt-exception @ so_mstr.so_stat
                              format "x(43)" WITH STREAM-IO /*GUI*/ .
                     else display
                        tt-apoExceptions.tt-exception @ so_mstr.so_stat
                           format "x(43)" WITH STREAM-IO /*GUI*/ .

                     delete tt-apoExceptions.
                  end.

               end. /* with frame b-apoatp */

            end.  /* Use Apo Atp */
            else
            display
               so_mstr.so_nbr
               so_mstr.so_cust
               so_mstr.so_ship
               so_mstr.so_po
               so_mstr.so_ord_date
               so_mstr.so_req_date
               so_mstr.so_due_date
               so_mstr.so_conf_date
               so_mstr.so_stat
         with frame b width 132 no-attr-space down STREAM-IO /*GUI*/ .
         end. /* edi-conf = no */

         /* REPORT BTB ERROR PROBLEMS - ONLY IF NON-EDI */
         if soc_use_btb and so_primary = yes
            and edi-conf = no then do:
            if can-find(first sod_det
                        where sod_det.sod_nbr   = so_nbr
                        and sod_det.sod_confirm = no)
            then do:

               display txt with frame c STREAM-IO /*GUI*/ .
               for each btb-wkfl where btb-mess-txt <> "" no-lock:

                  /* GET DEFAULT EMT TYPE FROM lngd_det */
                  {gplngn2a.i
                     &file = ""emt""
                     &field = ""btb-type""
                     &code = sod_det.sod_btb_type
                     &mnemonic = btb-type
                     &label = btb_type_desc}

                  display
                     btb-line
                     btb-req-date
                     btb-due-date
                     btb-type
                     btb-vend
                     btb-mess-txt
                  with frame d STREAM-IO /*GUI*/ .
               end.
            end. /* IF CAN-FIND(FIRST sod_det .. */
         end. /* if soc_use_btb and so_primary = yes and edi_conf = no */

         if l_atp_msg_needed = yes
         and edi-conf = no then do:
            for each tt-atp-msg where atp-msg <> "":
               display atp-msg @ l_msg with frame msg_frm STREAM-IO /*GUI*/ .
               down 1 with frame msg_frm.
            end.
         end.
      end. /* if LAST-OF (sod_det.sod_nbr) */
   end. /* if lines confirmed */

end.  /* for each so_mstr */

   for each unproc_file with frame fr-proc:
      /* SET EXTERNAL LABELS */
      setFrameLabels(frame fr-proc:handle).
      display
         unproc_db
         unproc_nbr
         unproc_line
         unproc_part
         unproc_site
         unproc_cause column-label {&sosocoa_p_6}
      with frame fr-proc down STREAM-IO /*GUI*/ .
   end.

if apoAtpOn and apoAtpDelAvail then do:
   /*Close the apoAtpStream */
   {pxrun.i &proc = 'closeApoAtpIOStream' &program = 'giapoxr.p'
        &handle=ph_giapoxr
        &catcherror = TRUE
        &noapperror = TRUE
        &module = 'GI1'
   }
end.

/* ========================================================================== */
/* ******************* I N T E R N A L  P R O C E D U R E S *******************/
/* ========================================================================== */

PROCEDURE checkATP:
   define output parameter  confirm-allowed as logical     no-undo
                                            initial yes.

   define variable  apoAtpSessionAvailable   as logical              no-undo.
   define variable  op-atp-ok      as logical              no-undo.
   define variable  ATP-enf-mode   like pt_atp_enforcement no-undo.
   define variable  pm-code        like pt_pm_code         no-undo.
   define variable  cum-amt-avail  like atp_qty            no-undo.
   define variable  ord-amt-avail  like atp_qty            no-undo.
   define variable  sug-due-date   as date                 no-undo.
   define variable  sug-dd-cum-atp-qty  like atp_qty       no-undo.
   define variable  atp-end-horizon-date as date           no-undo.
   define variable  reserved-inventory  like in_qty_oh     no-undo.
   define variable  changeDueDate            as logical              no-undo.
   define variable  dummyChar                as character            no-undo.
   define variable  errorConfComponents      as character            no-undo.
   define variable  stdAtpUsed               as logical              no-undo.

   define buffer    so_mstr_buf for so_mstr.
   define buffer    si_mstr for si_mstr.

   for first so_mstr_buf
   fields (so_cust so_ship so_bill )
   where recid(so_mstr_buf) = so_recno
   no-lock:

      for first si_mstr
         fields(si_site si_db)
      where si_site = sod_det.sod_site
      no-lock:

         if si_db <> so_db then do:
            /* Switch to the Inventory site */
            {gprun.i ""gpalias2.p"" "(sod_det.sod_site,output err-flag)"}
         end.

         {gprun.i ""soatpr.p""
            "(input  so_mstr_buf.so_cust,
              input  so_ship,
              input  so_bill,
              input  sod_det.sod_nbr,
              input  sod_line,
              input  sod_site,
              input  sod_part,
              input  sod_due_date,
              input  sod_um_conv,
              input  sod_um,
              input  sod_qty_ord,
              input  sod_btb_type,
              input  yes,
              input  no,
              input  soc_ctrl.soc_atp_enabled,
              input  soc_horizon,
              input  apoAtpOn,
              input  soUseStdAtpWhenNoApo,
              input  ""SO"",
              input  sod_type,
              input  sod_consume,
              input  apoAtpDelAvail,
              input  apoAtpDelAvailMsg,
              output op-atp-ok,
              output ATP-enf-mode,
              output pm-code,
              output cum-amt-avail,
              output ord-amt-avail,
              output sug-due-date,
              output sug-dd-cum-atp-qty,
              output atp-end-horizon-date,
              output reserved-inventory,
              output stdAtpUsed,
              output apoAtpSessionAvailable,
              output messageNumber,
              output errorConfComponents)"}

         /* Switch back to the SEO site */
         if si_db <> so_db then do:
            {gprun.i ""gpalias3.p"" "(so_db, output err-flag)" }
         end.

         /* If APO ATP is on and the APO Model was available, then track */
         /* the quote that was created so that it can be confirmed later */
         if op-atp-ok and apoAtpSessionAvailable then do:

            create tt-apoLinesConfirmed.
            assign
               tt-apoLinesConfirmed.tt-orderId = sod_nbr
               tt-apoLinesConfirmed.tt-orderLineId = sod_line
               tt-apoLinesConfirmed.tt-lineDueDate = sod_due_date
               tt-apoLinesConfirmed.tt-siteId = sod_site
               tt-apoLinesConfirmed.tt-itemId = sod_part
               tt-apoLinesConfirmed.tt-atpEnforcement = ATP-enf-mode.

            /* If a configured item did not exist in the model,        */
            /* then display warning message.                           */
            if errorConfComponents <> ""  then do:
               /* This clears the temp table tt-apoExceptions */
               {pxrun.i &proc = 'clearApoException'
                      &program = 'sosoxr1.p'
                      &handle=ph_sosoxr1
                      &catcherror = TRUE
                      &noapperror = TRUE}

               /* Message 4623 - These Components did not exist in the        */
               /* APO Model.                                                  */
               /* The number 43 after errorConfComponents is the max number   */
               /* of spaces available for displaying the apo atp exceptions.  */
               {pxrun.i &proc = 'createApoExceptionAppend'
                      &program = 'sosoxr1.p'
                      &handle=ph_sosoxr1
                      &param = "(input sod_nbr,
                                 input sod_line,
                                 input sod_due_date,
                                 input 4623,
                                 input '2',
                                 input errorConfComponents,
                                 input 43,
                                 input-output table tt-apoExceptions)"
                      &catcherror = TRUE
                      &noapperror = TRUE}
            end. /* errorConfComponents <> "" */

            return.
         end.

         else if not op-atp-ok and not stdAtpUsed then do:
            /* This clears the temp table tt-apoExceptions */
            {pxrun.i &proc = 'clearApoException'
                   &program = 'sosoxr1.p'
                   &handle=ph_sosoxr1
                   &catcherror = TRUE
                   &noapperror = TRUE}

            /* This clears the temp table tt-apoLinesConfirmed */
            {pxrun.i &proc = 'clearApoLinesConfirmed'
                   &program = 'sosoxr1.p'
                   &handle=ph_sosoxr1
                   &catcherror = TRUE
                   &noapperror = TRUE}

            /* Tne number 43 after errorConfComponents is the maximum number */
            /* of spaces available for displaying the apo atp exceptions.    */
            {pxrun.i &proc = 'processApoAtpBatchResultsNotOk'
               &program = 'sosoxr1.p'
               &handle=ph_sosoxr1
               &param = "(input sod_nbr,
                     input sod_line,
                     input sod_site,
                     input sod_part,
                     input (sod_qty_ord * sod_um_conv),
                     input sod_due_date,
                     input soatpwarn,
                     input soatperr,
                     input sug-due-date,
                     input apoAtpSessionAvailable,
                     input messageNumber,
                     input errorConfComponents,
                     input 43,
                     input ATP-enf-mode,
                     input 'SO',
                     input-output table tt-apoExceptions,
                     input-output table tt-apoLinesConfirmed,
                     output changeDueDate)"
               &catcherror = TRUE
               &noapperror = TRUE
               }

            if return-value = {&SUCCESS-RESULT} then do:
               /* The order line should be confirmed. */
               if changeDueDate then
                  sod_due_date = sug-due-date.
               return.
            end.
            confirm-allowed = no.
            return.
         end. /* not op-atp-ok and not stdAtpUsed */

         if apoAtpOn and
            stdAtpUsed and
            (ATP-enf-mode <> "0") then do:
         /* Track the reason the APO model could not be used                 */
         /* If the ATP Enforcement level is 0 (None), then we do not track   */
         /* messages.                                                        */
            dummyChar = "".
            /* If mesageNumber = 3888, then the error Cannot connect to      */
            /* the App Server occurred.                                      */
            if messageNumber = 3888 then do:
               for first mfc_ctrl no-lock where
                    mfc_ctrl.mfc_field = {&APP_SERVER_SERVICE_NAME}: end.
               if available mfc_ctrl then dummyChar =
                  mfc_ctrl.mfc_char.
            end.

            /* This clears the temp table tt-apoExceptions */
            {pxrun.i &proc = 'clearApoException'
                   &program = 'sosoxr1.p'
                   &handle=ph_sosoxr1
                   &catcherror = TRUE
                   &noapperror = TRUE}

            {pxrun.i &proc = 'createApoExceptionAppend'
               &program = 'sosoxr1.p'
               &handle = ph_sosoxr1
               &param = "(input sod_nbr,
                     input sod_line,
                     input sod_due_date,
                     input messageNumber,
                     input '2',
                     input dummyChar,
                     input 43,
                     input-output table tt-apoExceptions)"}

         end.
         /* if ATP not OK see if due date needs to be changed */
         if op-atp-ok = no and stdAtpUsed then do:
            /* Check and see if due date should be modified   */
            /* ATP enf mode values are:        */
            /*   0 = No ATP enf in effect      */
            /*   1 = ATP enf is warning only   */
            /*   2 = ATP enf is hard error     */
            if sug-due-date <> sod_due_date
            and ((ATP-enf-mode = "2" and soatperr  = yes)
            or (ATP-enf-mode = "1" and soatpwarn = yes))
            then do:
               sod_due_date = sug-due-date.
               /* Display line on report informing due date change */
               create tt-atp-msg.
               {pxmsg.i &MSGNUM=4563 &ERRORLEVEL=1  &MSGBUFFER=atp-msg}
               /* ATP ENFORCEMENT CALCULATED DUE DATE */
               assign
                  l_atp_msg_needed = yes
                  atp-msg = atp-msg
                    + " "
                    + string(sod_line)
                    + " "
                    + sod_part
                    + " "
                    + string(sod_due_date).
            end.  /* sug-due-date <> sod_due_date */
            else
            /* check and see if item should not be confirmed  */
            if ATP-enf-mode = "2" and soatperr  = no
            then do:
               create tt-atp-msg.
               {pxmsg.i &MSGNUM=4099 &ERRORLEVEL=4  &MSGBUFFER=atp-msg}
               /* QTY AVAILABLE NOT SUFFICIENT FOR DUE DATE */
               assign
                  l_atp_msg_needed = yes
                  confirm-allowed = no
                  atp-msg = atp-msg
                          + " "
                          + string(sod_line)
                          + " "
                          + sod_part
                          + " "
                          + string(sod_due_date).
            end.  /* if ATP-enf-mode = "2" */
            else
            /* Check and see if item should be accepted with warning */
            if ATP-enf-mode = "1" and soatpwarn  = no
            then do:
               create tt-atp-msg.
               {pxmsg.i &MSGNUM=4099 &ERRORLEVEL=2 &MSGBUFFER=atp-msg}
               /* QTY AVAILABLE NOT SUFFICIENT FOR DUE DATE */
               assign
                  l_atp_msg_needed = yes
                  atp-msg = atp-msg
                          + " "
                          + string(sod_line)
                          + " "
                          + sod_part
                          + " "
                          + string(sod_due_date).
            end. /* if ATP-enf-mode = "1" */
         end. /*  atp ok = no and standard used. */
      end. /*  for first si_mstr   */
   end.  /* for first so_mstr  */
END PROCEDURE.   /* checkATP */


PROCEDURE calcPromiseDate:
   define input parameter p-cust like cm_addr     no-undo.
   define input parameter p-site like pt_site     no-undo.
   define input-output parameter p-due-date
                           like sod_due_date      no-undo.
   define input-output parameter p-promise-date
                           like sod_promise_date  no-undo.

   define variable prev-promise-date like sod_promise_date no-undo.

   prev-promise-date = p-promise-date.

   /* Attempt to calculate promise date now... */
   /* Retrieve address record of ship-to customer */
   for first ad_mstr
      fields (ad_addr
              ad_ctry
              ad_state
              ad_city)
   where ad_addr = p-cust
   no-lock:

      for first si_mstr
         fields(si_site si_db)
      where si_site = p-site
      no-lock:

         /* Switch to the Inventory site */

         if si_db <> so_db then do:
            {gprun.i ""gpalias3.p"" "(si_db, output err-flag)" }
         end.

         {gprun.i ""sopromdt.p""
               "(input p-site,
                 input ad_ctry,
                 input ad_state,
                 input ad_city,
                 input """",
                 input-output p-due-date,
                 input-output p-promise-date)" }

         /* Switch back to the sales order site */
         if si_db <> so_db then do:
            {gprun.i ""gpalias3.p"" "(so_db, output err-flag)" }
         end.

      end. /* FOR FIRST si_mstr */
   end. /* for first ad_mstr */

   if p-promise-date <> ?
   and p-promise-date <> prev-promise-date
   then do:
      /* Display line on report informing promise date change */
      create tt-atp-msg.
      {pxmsg.i &MSGNUM=4568 &ERRORLEVEL=1 &MSGBUFFER=atp-msg}
      /* CALCULATED PROMISE DATE */
      assign
         l_atp_msg_needed = yes
         atp-msg = atp-msg
                 + " "
                 + string(sod_det.sod_line)
                 + " "
                 + sod_part
                 + " "
                 + string(p-promise-date).
   end.  /* if p-promise-date <> prev-promise-date */
END PROCEDURE.   /* calcPromiseDate */


PROCEDURE updateOrderConfirmDate:
define buffer somstrbf for so_mstr.

if not db-error and all_confrm then do:
   for first somstrbf
   fields (so_conf_date)
   where recid(somstrbf) = so_recno
   exclusive-lock:
      somstrbf.so_conf_date = today.
	  somstrbf.so_user1 = global_userid.
   end.
end.
END PROCEDURE.   /* updateOrderConfirmDate */


PROCEDURE checkForSoMstrLocked:
define input  parameter pOrdno         like so_nbr     no-undo.
define output parameter pOrderLocked   as logical      no-undo.

define  buffer   somstr        for so_mstr.


pOrderLocked = no.

find somstr where so_nbr = pOrdno exclusive-lock no-wait no-error.

if locked somstr
   then pOrderLocked = yes.
END PROCEDURE.   /* checkForSoMstrLocked */

PROCEDURE setupApoAtp:

/* -----------------------------------------------------------------------------
Purpose: If system is set to use APO ATP, then verify demand listener program
         can be found and if it can be found, then open the APO ATP shared
         stream.
Exceptions:
Conditions:

History:
------------------------------------------------------------------------------*/

define variable apoAtpDelAvailField as character no-undo.

/* SET the apoAtpOn Flag - If Yes, then use APO ATP */
if not can-find(mfc_ctrl where mfc_field = {&USE_APO_ATP_FOR_SO}
   and mfc_logical)
then leave.
apoAtpOn = yes.

{pxrun.i &proc = 'validateDemandListenerExists' &program = 'giapoxr.p'
    &handle = ph_giapoxr
    &param = "(input 'SO',
               output apoAtpDelAvailMsg,
               output apoAtpDelAvailField)"
    &catcherror = true
    &noapperror = true
    &module = 'GI1'}

if return-value <> {&SUCCESS-RESULT} then do:
   apoAtpDelAvail = no.
   leave.
end.

/* Open the apoAtpStream */
{pxrun.i &proc = 'openApoAtpIOStream' &program = 'giapoxr.p'
         &handle=ph_giapoxr
         &catcherror = TRUE
         &noapperror = TRUE
         &module = 'GI1'
}

END PROCEDURE. /* setupApoAtp */
